package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.Dto.DishDto;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveAndFl(dishDto);
        return R.success("新增成功");
    }


    @GetMapping("/page")
    public R<Page> SecletAll(int page, int pageSize, String name){
        //构造分页构造器对象
        Page<Dish> dishPage = new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage =new Page<>();
        //条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(name != null,Dish::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        //执行分页查询
        dishService.page(dishPage,queryWrapper);
        //对象拷贝
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
        List<Dish> records = dishPage.getRecords();
        List<DishDto> list = records.stream().map((item)->{

            DishDto dishDto = new DishDto();
            //copy
            BeanUtils.copyProperties(item,dishDto);
            //根据id查询对象
            Long categoryId = item.getCategoryId();

            Category category = categoryService.getById(categoryId);
            if (category != null){
                String name1 = category.getName();
                dishDto.setCategoryName(name1);
            }
            return dishDto;
        }).collect(Collectors.toList());
        dishDtoPage.setRecords(list);
        return R.success(dishDtoPage);
    }

    @DeleteMapping
    public R<String> del(@RequestParam List<Long> ids){
        //removeByIds是集合类型的id
        //removeById是单个的id
        dishService.removeByIds(ids);
        return R.success("删除成功");
    }

    @GetMapping("/{id}")
    public R<DishDto> Updata(@PathVariable Long id){

        DishDto byIdWithF = dishService.SelectBI(id);

        return R.success(byIdWithF);
    }

    @PutMapping
    public R<String> updateWithFlavor(@RequestBody DishDto dishDto){

        dishService.updateWithFlavor(dishDto);

        return R.success("修改成功");
    }

    //起售，停售
    @PostMapping("/status/{status}")
    public R<String> upstatus(@RequestParam Long[] ids){
        for (Long id : ids) {
            Dish dish = dishService.getById(id);
            Integer status = dish.getStatus();
            status = (status==0 ? 1:0);
            dish.setStatus(status);
            dishService.updateById(dish);
        }

        return R.success("修改状态成功");
    }


    //分类查询id
    @GetMapping("/list")
    public R<List<DishDto>> select(Dish dish){
        //条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId() != null ,
                Dish::getCategoryId,dish.getCategoryId());

        //添加过滤条件，查询状态为1（起售状态）的菜品
        queryWrapper.eq(Dish::getStatus,1);
        //添加排序条件
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        List<Dish> list = dishService.list(queryWrapper);

           List<DishDto> dishes = list.stream().map((item)->{
               DishDto dishDto = new DishDto();
               BeanUtils.copyProperties(item,dishDto);

               Long categoryId = item.getCategoryId();

               Category category = categoryService.getById(categoryId);

               if (category != null){
                   String name = category.getName();
                   dishDto.setCategoryName(name);
               }

               Long dishid = item.getId();
               LambdaQueryWrapper<DishFlavor> queryWrapper1 = new LambdaQueryWrapper<>();
               queryWrapper1.eq(DishFlavor::getDishId,dishid);

               List<DishFlavor> dishFlavors = dishFlavorService.list(queryWrapper1);
               dishDto.setFlavors(dishFlavors);

               return dishDto;
           }).collect(Collectors.toList());

           return R.success(dishes);
    }
}
